/*  
  -- The Lime II Project -- 

  A tuplespaces-based middleware for coordinating agents and mobile hosts.
  Copyright (C) 2005.
  Gian Pietro Picco, Amy L. Murphy, Lorenzo Bellini.

  This library is free software; you can redistribute it and/or
  modify it under the terms of the GNU Lesser General Public
  License as published by the Free Software Foundation; either
  version 2.1 of the License, or (at your option) any later version.

  This library is distributed in the hope that it will be useful,
  but WITHOUT ANY WARRANTY; without even the implied warranty of
  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  Lesser General Public License for more details.

  You should have received this copy of the GNU Lesser General Public
  License along with this library; if not, write to the Free Software
  Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA 
 */

package sagroup.dracots.testing;

import lime2.Location;
import sagroup.dracots.DracoTS;
import sagroup.dracots.DracoTuple;
import sagroup.dracots.ShareTuple;

/**
 * A simple agent
 * 
 * @author Huang Xiaodong
 */

public class DisplayAllLocalReplica implements Runnable {
	/**
	 * Constructor method (default). Destination location is here.
	 */
	public DisplayAllLocalReplica() {
	}

	/**
	 * Run method.
	 */
	public void run() {
		System.out.println("DisplayAllLocalReplica agent started");
		try {
			// Here to get a DracoTupleSpace instance named testing.
			DracoTS dts = DracoTS.get("testing");
			System.out.println("Get DracoTS.");

			// Turn on the monitor to keep record of operations
			dts.setMonitorStatus(false);
			System.out.println("Turn on monitor.");

			displayAllLocalReplicaTuples(dts);

			System.out.println("Agent done!");
		} catch (Exception e) {
			System.out.println("Operation failed!");
			e.printStackTrace();
		}
	}

	public void displayAllLocalReplicaTuples(DracoTS dts) {
		ShareTuple st = ShareTuple.createVIDTemplate(null);
		st.setCurr(Location.HERE);
		st.setDest(Location.HERE);
		st.setReplica(true);
		DracoTuple[] dt = dts.rdg(st);
		System.out.println("Display all tuples in the tuplespace:");
		if (dt != null) {
			for (int i = 0; i < dt.length; ++i)
				System.out.println(dt[i]);
		} else
			System.out.println("No tuples found.");
	}

}